package Demo1;

public class T8 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("线程运行中");
                try {
                    Thread.sleep(100);//被提前唤醒将抛出异常
                } catch (InterruptedException e) {
                    System.out.println("线程被中断");
                    // 捕获到中断异常后，重新设置自己的中断状态，
                    // 因为 InterruptedException 抛出时中断状态会被清除。
                    Thread.currentThread().interrupt();
                    break;
                }
            }
            System.out.println("线程执行完毕!");
        });
        thread.start();

        System.out.println("让线程结束");
        // 调用中断操作，设置线程thread的中断标志为 true
        thread.interrupt();
    }
}
